﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Web;
using System.IO;

namespace Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        const int BUFFER_SIZE = 4096;
        string filePath = string.Empty;
        string fileName = string.Empty;
        
        string ftpServerAddress = "ftp://sal-cairns.jcu.edu.au/";
        string ftpUsername = "nordic-sauna-ftp";
        string ftpPassword = "upload";

        private void browseButton_Click(object sender, EventArgs e)
        {
            //DialogResult result = uploadOpenFileDialog.ShowDialog();
            if (uploadOpenFileDialog.ShowDialog() == DialogResult.OK)
            {
                fileNameTextBox.Text = uploadOpenFileDialog.FileName;

                filePath = uploadOpenFileDialog.FileName;
                fileName = uploadOpenFileDialog.SafeFileName;
            }
        }

        private void uploadFTPbtn_Click(object sender, EventArgs e)
        {
            if (filePath != string.Empty)
            {
                FtpWebRequest request;
                FtpWebResponse response;

                Stream sourceStream = new MemoryStream();
                Stream requestStream = sourceStream;

                System.Diagnostics.Debug.WriteLine("Server: " + ftpServerAddress);
                System.Diagnostics.Debug.WriteLine("User: " + ftpUsername);
                System.Diagnostics.Debug.WriteLine("Password: " + ftpPassword);
                System.Diagnostics.Debug.WriteLine("Filepath: " + filePath);
                System.Diagnostics.Debug.WriteLine("Filename: " + fileName);
                System.Diagnostics.Debug.WriteLine("URI: " + ftpServerAddress + '/' + fileName);
                
                try
                {
                    request = (FtpWebRequest)WebRequest.Create(ftpServerAddress + '/' + fileName);
                    request.Method = WebRequestMethods.Ftp.UploadFile;
                    request.Credentials = new NetworkCredential(ftpUsername, ftpPassword);

                    sourceStream = new FileStream(filePath, FileMode.Open);

                    requestStream = request.GetRequestStream();
                    request.ContentLength = sourceStream.Length;

                    byte[] buffer = new byte[BUFFER_SIZE];
                    int bytesRead = sourceStream.Read(buffer, 0, BUFFER_SIZE);

                    do
                    {
                        requestStream.Write(buffer, 0, bytesRead);
                        bytesRead = sourceStream.Read(buffer, 0, BUFFER_SIZE);

                    } while (bytesRead > 0);

                    sourceStream.Close();
                    requestStream.Close();

                    response = (FtpWebResponse)request.GetResponse();
                    //processResultToolStripStatusLabel.Text = string.Format("Upload Complete, Status Code: {0}", response.StatusDescription);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.GetType().Name + ": \n" + ex.Message, "Exception!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    request = null;

                    sourceStream.Close();
                    sourceStream = null;

                    requestStream.Close();
                    requestStream = null;
                }
            }
        }
    }
}
